import os,fnmatch,sys,string
def mininaljs(root,patterns='*',single_level=False,yield_folders=False):
    patterns = patterns.split(';');
    newPath = "";
    createdObject = False;
    #print(root);
    
    for path,subdirs,files in os.walk(root):
        if yield_folders:
            files.extend(subdirs);

        files.sort();
        for name in files:
            newPath = "D"+(path[1:len(path)]);
            
            if(path.find("CVS")>-1):
                continue;
            
            if(not os.path.exists(newPath)):
                os.makedirs(newPath);
            
            for pattern in patterns:
                if fnmatch.fnmatch(name,pattern):
                    createObject = False;
                    syscmd = 'java -jar  D:\yuicompressor-2.4.2\\build\yuicompressor-2.4.2.jar  --charset utf8 --line-break 250 '+os.path.join(path,name)+ ' -o '+ os.path.join(newPath,name);
                    #print syscmd;
                    os.system(syscmd);
                    
                    break;
                    
                    #os.system();                    
                    #output_file.write(input_file.readline());
                    #output_file.write("import org.apache.commons.logging.Log;");
                    #output_file.write(os.linesep);                                        
                    #output_file.write("import org.apache.commons.logging.LogFactory;");
                    #output_file.write(os.linesep);                    
                    break;

        if single_level:
            break;
    
            
if __name__=='__main__':
    #all_files('E:\workshop\company\unsins\WebApp\WEB-INF\page','*.jsp',search_text='<%=path%>/sitechannel/service.do?method=toService',replace_text='<%=path%>/statichtml/service/toService.html');
    #mininaljs('E:\workshop\company\unscard\webapp\js','*.js');
    mininaljs('E:\workshop\company\unscard\webapp\page\\frontsystem\css','*.css');    
##    for path in thefiles:
##        print path;
##    
    #print 'ok'+ 'adasdasdas';
    #os.system('java -jar  D:\yuicompressor-2.4.2\\build\yuicompressor-2.4.2.jar  E:\workshop\company\unsins\WebApp\js\popcalendar.js -o d:\minjs\popcalendar.js');

